
<template>



<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="赛事名称" prop="name">
  <el-input v-model="ruleForm.name" placeholder="请输入赛事名称" style="width:600px;"></el-input>
</el-form-item>

  <el-form-item label="举办地区" prop="region">
      <div id="app">
        <el-cascader
          size="large"
          :options="pcaTextArr"
          v-model="ruleForm.region"
          placeholder="请选择赛事举办地区">
        </el-cascader>
      </div>
  </el-form-item>
  <el-form-item label="详细地址" prop="address">
    <el-input  v-model="ruleForm.address" placeholder="请输入详细地址(XX街道)" style="width:600px;" ></el-input>
  </el-form-item>

<el-form-item label="开始时间" required>
  <el-col :span="11">
    <el-form-item prop="startTime">
      <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.startTime" :picker-options="pickerOptions" @change="changeData(true)" style="width: 400px;"></el-date-picker>
    </el-form-item>
  </el-col>
</el-form-item>
  <el-form-item label="截止时间" required>
    <el-col :span="11">
      <el-form-item prop="endTime">
        <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.endTime" :picker-options="pickerOptions" @change="changeData(true)" style="width: 400px;"></el-date-picker>
      </el-form-item>
    </el-col>
  </el-form-item>
  <el-form-item label="年龄段" required prop="ageType">
    <el-input  v-model="ruleForm.ageType"  placeholder="请输入年龄段(XX岁以上/XX岁至XX岁)" style="width:350px;" ></el-input>
  </el-form-item>

<el-form-item label="备注" prop="remark">
  <el-input type="textarea" autosize v-model="ruleForm.remark" placeholder="1500个字符以内" style="width:600px;"></el-input>
</el-form-item>
<el-form-item>
  <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
  <el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>

</template>




<script>
import { pcaTextArr } from 'element-china-area-data'
import {addCompetition} from "@/api/competition/Competition";
export default {
  data() {
    return {
      input: '',
      pcaTextArr,
      // selectedOptions: [],
      ruleForm: {
        name: '',
        region: [],
        address :'',
        startTime: '',
        endTime: '',
        ageType:'',
        remark: '',
        userId:this.$store.state.user.id,
      },
      pickerOptions: {
        disableDate: time => {
          return time.getTime() > Date.now();
        }
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '请选择活动区域', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入具体地址', trigger: 'change' },
          { pattern: /^(?!.*[省市区]).*$/, message: '请输入具体地址,不包含 "省,市,区"'}
        ],
        ageType: [
          { required: true, message: '请输入相应年龄段', trigger: 'blur' },
          { pattern: /^((\d{2})岁以上|(\d{2})岁至(\d{2})岁)$/, message: '请输入年龄段,"XX岁以上" 或 "XX岁至XX岁"'}
        ],
        startTime: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        endTime: [
          { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
        ],
        remark: [
          { message: '请选择时间', trigger: 'change' },
          {max: 1500, message: '1500个字符以内', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    changeData(flag) {
      if (flag) {
        if (this.ruleForm.endTime) {
          if (this.ruleForm.startTime > this.ruleForm.endTime) {
            this.ruleForm.startTime = null;
            this.ruleForm.endTime = null;
            this.$message.error("开始时间不能大于结束时间！时间已清空！请重新选择!");
          }
        }
      }
    },
    submitForm() {
      this.$refs["ruleForm"].validate((valid) => {
        console.log('提交')
        console.log(this.ruleForm)
        if(valid){
          addCompetition(this.ruleForm).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;

          });
        }else{
          alert("您还有选项没有填写!")
        }
      });
    },

  }
}
</script>
